package cq.zhcqk.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.zzhoujay.richtext.RichText;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;

/**
 * Created by zhangle on 2017/12/11.
 */
public class SQ_SqjjActivity extends BaseActivity implements View.OnClickListener{
    private RichText richText;
    private TextView text_test;
    private String type;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_sq_wjdc);
        type = getIntent().getStringExtra("type");
        initBack();
        RichText.initCacheDir(context);
        text_test = (TextView)findViewById(R.id.gz_detail);
    }

    @Override
    protected void initData() {
        getDetail();
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);


        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
        }
    }

    private String relaceUrl(String content){
        String info = content.replace("<img src=\"/","<img src=\""+Common.URL+"/");
        System.out.println(info);
        return info;
    }

    private void getDetail(){
        RequestParams params = new RequestParams();
        params.add("type", type);
        httpClient.post(Common.URL+Common.ONE,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(map != null && map.size()>0){
                    Map<String, Object> mapData = (Map<String, Object>) map.get("data");
                    if(mapData != null && mapData.size()>0){
                        String content = mapData.get("content")+"";
                        if(content == null || "".equals(content) || "null".equals(content)){
                            return;
                        }
                        richText = RichText.from(relaceUrl(content)).into(text_test);
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("responseString："+responseString);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
